﻿using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.DOCUMENT.Components;
using ASPNETVN.PORTAL.Components.Share.Controls;
using ASPNETVN.PORTAL.Components.Share;
using Type=ASPNETVN.PORTAL.Modules.DOCUMENT.Components.Type;
using System.Web;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.Controls
{
    public partial class Detail : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // BEGIN CASE: Multiple categories per one item.
                if (string.IsNullOrEmpty(Request.QueryString["documentcode"]) == false)
                {
                    this.ItemID = Components.Item.GetItemID_ByCode(HttpUtility.UrlDecode(Request.QueryString["documentcode"]));
                }
                // END CASE: Multiple categories per one item.

                Components.Item item = Components.Item.Load(this.ItemID);
                if (item != null)
                {
                    // Build page link.
                    PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");

                    int categoryID = item.CategoryID;
                    Category category = Category.Load(categoryID);
                    if (category != null)
                    {
                        Category parentCategory = Category.Load(category.ParentID);
                        if (parentCategory != null)
                        {
                            pagelink.AddLink(parentCategory.Title, "#");

                            string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID));
                            pagelink.AddLink(category.Title, url);

                            pagelink.AddLink(item.Code, "#");
                        }
                    }

                    lblTitle.Text = item.Code;
                    lblType.Text = Type.Load(item.TypeID).Title;
                    lblPublisher.Text = Publisher.Load(item.PublisherID).Title;
                    lblPublishedDate.Text = item.PublishedDate.ToShortDateString();
                    lblAvailableDate.Text = item.AvailableDate.ToShortDateString();
                    lblAvailableDate.Visible = item.AvailableDate > new DateTime(1753, 1, 1);
                    lblSignedBy.Text = item.SignedBy;
                    lblSumary.Text = Server.HtmlDecode(item.Summary);
                }
            }
        }
    }
}